#include "FPSController.h"
#include "Device.h"

FPSController* FPSController::m_pInstance = 0;

FPSController::FPSController()
: m_iFrame(0), m_iLimitFPS(0), m_iLimitFrame(0), m_iStartTime(0)
{
	this->SetLimitFPS(DEFAULT_LIMIT_FPS);
}

FPSController::~FPSController()
{}

__VOID FPSController::SetLimitFPS(__UINT32 limitFPS)
{
	m_iLimitFPS = limitFPS;
	m_iLimitFrame = 1000 / limitFPS;
}

__VOID FPSController::StartCounter()
{
	m_iStartTime = Device::GetInstance()->GetTimer();
}

__VOID FPSController::EndCounter()
{
	__UINT32 iEndTime = Device::GetInstance()->GetTimer();
	__UINT32 iDeltaTime = __UINT32(iEndTime - m_iStartTime);
	if (iDeltaTime < m_iLimitFrame)
	{
		m_iFrame = m_iLimitFrame;
		Device::GetInstance()->SleepEx(m_iLimitFrame - iDeltaTime);
	}
	else
	{
		m_iFrame = iDeltaTime;
		Device::GetInstance()->SleepEx(1);
	}
}